<template>
  <div class="show-annex">
    <div class="imageBox" @click="hiddenImg" v-if="(fileType == 'png') || (fileType == 'jpg') || (fileType == 'jpeg')">
      <span class="vux-close" @click="hiddenImg"></span>
      <div class="imageContent">
        <img :src="option.fileUrl" alt="" id="img">
      </div>
    </div>
    <div v-if="fileType == 'pdf'" class="pdf">
      <span class="vux-close" @click="hiddenImg"></span>
      {{currentPage}} / {{pageCount}}
      <pdf :src="option.fileUrl"
        :page="page"
        @num-pages="pageCount = $event"
			  @page-loaded="currentPage = $event"
        @link-clicked="page = $event"
      ></pdf>
      <div class="btn-box">
        <button @click="cutPage">上一页</button>
        <button @click="addPage">下一页</button>
      </div>
    </div>
    <div class="txtBox" @click="hiddenImg" v-if="fileType == 'txt'">
      <span class="vux-close" @click="hiddenImg"></span>
      <div id="txtContent">
        <p v-for="item in txtList">{{item}}</p>
      </div>
    </div>
  </div>
</template>

<script>
import pdf from 'vue-pdf';
export default {
  name: "show-annex",
   components: {
    pdf
  },
  props: {
    option: {
      type: Object,
      default: {}
    },
    hiddenImg: {
      type: Function,
      default: () => any
    }
  },
  mounted() {
    this.fileType = this.option.cFileDescr.toLowerCase();
    if (this.fileType == 'txt') {
      const _this = this;
      let xhr = new window.XMLHttpRequest();
      xhr.open('GET', this.option.fileUrl, true);
      xhr.responseType = 'arraybuffer';
      xhr.onload = function() {
        if (this.status == 200) {
          let blob = new window.Blob([this.response], {'type': 'text/plain'});
          const reader = new FileReader();
          reader.onload = function(e){
            _this.txtList = e.target.result.split("\r\n");
          };
          reader.readAsText(blob, 'gbk');
        }
      };
      xhr.send();
    }
  },
  data() {
    return {
      currentPage: 0,
      pageCount: 0,
      page: 1,
      txtList: [],
      fileType: '',
    };
  },
  methods: {
    cutPage() {
      const thisPage = this.page;
      if (thisPage < 2) return;
      this.page = thisPage-1;
    },
    addPage(){
      const thisPage = this.page;
      if (thisPage > (this.pageCount - 1)) return;
      this.page = thisPage+1;
    }
  }
};
</script>

<style lang="less">
@import "~vux/src/styles/close.less";
</style>

<style type="text/scss" lang="scss">
.show-annex {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #000;
  z-index: 999;
  .vux-close {
    position: absolute;
    right: 15px;
    top: 15px;
  }
  .imageBox {
    width: 100%;
    height: 100%;
    position: relative;
    .imageContent {
      // position: absolute;
      display: flex;
      width: 100%;
      height: 100%;
      align-items: center;
      img {
        width: 100%;
      }
    }
  }
  .pdf{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 99;
    background-color: #fff;
    padding-top: 20px;
    .btn-box{
      position: absolute;
      bottom: 0;
      padding: 15px 0;
      text-align: center;
      width: 100%;
      button{
        padding: 10px 15px;
        margin: 0 10px;
      }
    }
  }
  .txtBox{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #fff;
    padding: 20px;
    padding-top: 30px;
    p{
      line-height: 1.6;
      text-align: left;
    }
  }
}

</style>
